home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / DynaLoader.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  18.6 KB  |  600 lines

  1. package DynaLoader;
  2.  
  3.  
  4.  
  5. use vars qw($VERSION);
  6.  
  7. $VERSION = "1.02";
  8.  
  9. require Carp;
  10. require Config;
  11.  
  12. require AutoLoader;
  13. *AUTOLOAD = \&AutoLoader::AUTOLOAD;
  14.  
  15. $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
  16.  
  17.  
  18. sub dl_load_flags { 0x00 }
  19.  
  20.  
  21. ($dl_dlext, $dlsrc)
  22.     = @Config::Config{'dlext', 'dlsrc'};
  23.  
  24. $do_expand = $Is_VMS = $^O eq 'VMS';
  25.  
  26. @dl_require_symbols = ();       # names of symbols we need
  27. @dl_resolve_using   = ();       # names of files to link with
  28. @dl_library_path    = ();       # path to look for files
  29. @dl_librefs         = ();       # things we have loaded
  30. @dl_modules         = ();       # Modules we have loaded
  31.  
  32. @dl_resolve_using = dl_findfile('-lc') if $dlsrc eq "dl_dld.xs";
  33.  
  34. push(@dl_library_path, split(' ',$Config::Config{'libpth'}));
  35.  
  36. push(@dl_library_path, split(/:/, $ENV{LD_LIBRARY_PATH}))
  37.     if $ENV{LD_LIBRARY_PATH};
  38.  
  39.  
  40. boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader);
  41.  
  42.  
  43. if ($dl_debug) {
  44.     print STDERR "DynaLoader.pm loaded (@INC, @dl_library_path)\n";
  45.     print STDERR "DynaLoader not linked into this perl\n"
  46.         unless defined(&boot_DynaLoader);
  47. }
  48.  
  49. 1; # End of main code
  50.  
  51.  
  52.  
  53. sub bootstrap {
  54.     local(@args) = @_;
  55.     local($module) = $args[0];
  56.     local(@dirs, $file);
  57.  
  58.     Carp::confess("Usage: DynaLoader::bootstrap(module)") unless $module;
  59.  
  60.     Carp::croak("Can't load module $module, dynamic loading not available in this perl.\n".
  61.     "  (You may need to build a new perl executable which either supports\n".
  62.     "  dynamic loading or has the $module module statically linked into it.)\n")
  63.     unless defined(&dl_load_file);
  64.  
  65.     my @modparts = split(/::/,$module);
  66.     my $modfname = $modparts[-1];
  67.  
  68.     $modfname = &mod2fname(\@modparts) if defined &mod2fname;
  69.  
  70.     my $modpname = join('/',@modparts);
  71.  
  72.     print STDERR "DynaLoader::bootstrap for $module ",
  73.         "(auto/$modpname/$modfname.$dl_dlext)\n" if $dl_debug;
  74.  
  75.     foreach (@INC) {
  76.     chop($_ = VMS::Filespec::unixpath($_)) if $Is_VMS;
  77.     my $dir = "$_/auto/$modpname";
  78.     next unless -d $dir; # skip over uninteresting directories
  79.  
  80.     last if ($file=_check_file("$dir/$modfname.$dl_dlext"));
  81.  
  82.     push(@dirs, "-L$dir");
  83.     }
  84.     $file = dl_findfile(@dirs, map("-L$_",@INC), $modfname) unless $file;
  85.  
  86.     Carp::croak("Can't find loadable object for module $module in \@INC (@INC)")
  87.     unless $file;
  88.  
  89.     my $bootname = "boot_$module";
  90.     $bootname =~ s/\W/_/g;
  91.     @dl_require_symbols = ($bootname);
  92.  
  93.     my $bs = $file;
  94.     $bs =~ s/(\.\w+)?$/\.bs/; # look for .bs 'beside' the library
  95.     if (-s $bs) { # only read file if it's not empty
  96.         print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
  97.         eval { do $bs; };
  98.         warn "$bs: $@\n" if $@;
  99.     }
  100.  
  101.  
  102.     my $libref = dl_load_file($file, $module->dl_load_flags) or
  103.     Carp::croak("Can't load '$file' for module $module: ".dl_error()."\n");
  104.  
  105.     push(@dl_librefs,$libref);  # record loaded object
  106.  
  107.     my @unresolved = dl_undef_symbols();
  108.     Carp::carp("Undefined symbols present after loading $file: @unresolved\n")
  109.         if @unresolved;
  110.  
  111.     my $boot_symbol_ref = dl_find_symbol($libref, $bootname) or
  112.          Carp::croak("Can't find '$bootname' symbol in $file\n");
  113.  
  114.     my $xs = dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file);
  115.  
  116.     push(@dl_modules, $module); # record loaded module
  117.  
  118.     &$xs(@args);
  119. }
  120.  
  121.  
  122. sub _check_file {   # private utility to handle dl_expandspec vs -f tests
  123.     my($file) = @_;
  124.     return $file if (!$do_expand && -f $file); # the common case
  125.     return $file if ( $do_expand && ($file=dl_expandspec($file)));
  126.     return undef;
  127. }
  128.  
  129.  
  130. __END__
  131.  
  132.  
  133. sub dl_findfile {
  134.     my (@args) = @_;
  135.     my (@dirs,  $dir);   # which directories to search
  136.     my (@found);         # full paths to real files we have found
  137.     my $dl_ext= $Config::Config{'dlext'}; # suffix for perl extensions
  138.     my $dl_so = $Config::Config{'so'};    # suffix for shared libraries
  139.  
  140.     print STDERR "dl_findfile(@args)\n" if $dl_debug;
  141.  
  142.     arg: foreach(@args) {
  143.         if ($Is_VMS && m%[:>/\]]% && -f $_) {
  144.         push(@found,dl_expandspec(VMS::Filespec::vmsify($_)));
  145.         last arg unless wantarray;
  146.         next;
  147.         }
  148.         elsif (m:/: && -f $_ && !$do_expand) {
  149.         push(@found,$_);
  150.         last arg unless wantarray;
  151.         next;
  152.     }
  153.  
  154.         if (m:^-L:) { s/^-L//; push(@dirs, $_); next; }
  155.  
  156.         if (m:/: && -d $_) {   push(@dirs, $_); next; }
  157.  
  158.         if ($Is_VMS && /[:>\]]/ && -d $_) {   push(@dirs, $_); next; }
  159.  
  160.         my(@names, $name);    # what filenames to look for
  161.         if (m:-l: ) {          # convert -lname to appropriate library name
  162.             s/-l//;
  163.             push(@names,"lib$_.$dl_so");
  164.             push(@names,"lib$_.a");
  165.         } else {                # Umm, a bare name. Try various alternatives:
  166.             push(@names,"$_.$dl_ext")    unless m/\.$dl_ext$/o;
  167.             push(@names,"$_.$dl_so")     unless m/\.$dl_so$/o;
  168.             push(@names,"lib$_.$dl_so")  unless m:/:;
  169.             push(@names,"$_.a")          if !m/\.a$/ and $dlsrc eq "dl_dld.xs";
  170.             push(@names, $_);
  171.         }
  172.         foreach $dir (@dirs, @dl_library_path) {
  173.             next unless -d $dir;
  174.             chop($dir = VMS::Filespec::unixpath($dir)) if $Is_VMS;
  175.             foreach $name (@names) {
  176.         my($file) = "$dir/$name";
  177.                 print STDERR " checking in $dir for $name\n" if $dl_debug;
  178.         $file = _check_file($file);
  179.         if ($file) {
  180.                     push(@found, $file);
  181.                     next arg; # no need to look any further
  182.                 }
  183.             }
  184.         }
  185.     }
  186.     if ($dl_debug) {
  187.         foreach(@dirs) {
  188.             print STDERR " dl_findfile ignored non-existent directory: $_\n" unless -d $_;
  189.         }
  190.         print STDERR "dl_findfile found: @found\n";
  191.     }
  192.     return $found[0] unless wantarray;
  193.     @found;
  194. }
  195.  
  196.  
  197. sub dl_expandspec {
  198.     my($spec) = @_;
  199.  
  200.  
  201.     my $file = $spec; # default output to input
  202.  
  203.     if ($Is_VMS) { # dl_expandspec should be defined in dl_vms.xs
  204.     Carp::croak("dl_expandspec: should be defined in XS file!\n");
  205.     } else {
  206.     return undef unless -f $file;
  207.     }
  208.     print STDERR "dl_expandspec($spec) => $file\n" if $dl_debug;
  209.     $file;
  210. }
  211.  
  212. sub dl_find_symbol_anywhere
  213. {
  214.     my $sym = shift;
  215.     my $libref;
  216.     foreach $libref (@dl_librefs) {
  217.     my $symref = dl_find_symbol($libref,$sym);
  218.     return $symref if $symref;
  219.     }
  220.     return undef;
  221. }
  222.  
  223. =head1 NAME
  224.  
  225. DynaLoader - Dynamically load C libraries into Perl code
  226.  
  227. dl_error(), dl_findfile(), dl_expandspec(), dl_load_file(), dl_find_symbol(), dl_find_symbol_anywhere(), dl_undef_symbols(), dl_install_xsub(), dl_load_flags(), bootstrap() - routines used by DynaLoader modules
  228.  
  229. =head1 SYNOPSIS
  230.  
  231.     package YourPackage;
  232.     require DynaLoader;
  233.     @ISA = qw(... DynaLoader ...);
  234.     bootstrap YourPackage;
  235.  
  236.     sub dl_load_flags { 0x01 }     
  237.  
  238.  
  239. =head1 DESCRIPTION
  240.  
  241. This document defines a standard generic interface to the dynamic
  242. linking mechanisms available on many platforms.  Its primary purpose is
  243. to implement automatic dynamic loading of Perl modules.
  244.  
  245. This document serves as both a specification for anyone wishing to
  246. implement the DynaLoader for a new platform and as a guide for
  247. anyone wishing to use the DynaLoader directly in an application.
  248.  
  249. The DynaLoader is designed to be a very simple high-level
  250. interface that is sufficiently general to cover the requirements
  251. of SunOS, HP-UX, NeXT, Linux, VMS and other platforms.
  252.  
  253. It is also hoped that the interface will cover the needs of OS/2, NT
  254. etc and also allow pseudo-dynamic linking (using C<ld -A> at runtime).
  255.  
  256. It must be stressed that the DynaLoader, by itself, is practically
  257. useless for accessing non-Perl libraries because it provides almost no
  258. Perl-to-C 'glue'.  There is, for example, no mechanism for calling a C
  259. library function or supplying arguments.  It is anticipated that any
  260. glue that may be developed in the future will be implemented in a
  261. separate dynamically loaded module.
  262.  
  263. DynaLoader Interface Summary
  264.  
  265.   @dl_library_path
  266.   @dl_resolve_using
  267.   @dl_require_symbols
  268.   $dl_debug
  269.   @dl_librefs
  270.   @dl_modules
  271.                                                   Implemented in:
  272.   bootstrap($modulename)                               Perl
  273.   @filepaths = dl_findfile(@names)                     Perl
  274.   $flags = $modulename->dl_load_flags                  Perl
  275.   $symref  = dl_find_symbol_anywhere($symbol)          Perl
  276.  
  277.   $libref  = dl_load_file($filename, $flags)           C
  278.   $symref  = dl_find_symbol($libref, $symbol)          C
  279.   @symbols = dl_undef_symbols()                        C
  280.   dl_install_xsub($name, $symref [, $filename])        C
  281.   $message = dl_error                                  C
  282.  
  283. =over 4
  284.  
  285. =item @dl_library_path
  286.  
  287. The standard/default list of directories in which dl_findfile() will
  288. search for libraries etc.  Directories are searched in order:
  289. $dl_library_path[0], [1], ... etc
  290.  
  291. @dl_library_path is initialised to hold the list of 'normal' directories
  292. (F</usr/lib>, etc) determined by B<Configure> (C<$Config{'libpth'}>).  This should
  293. ensure portability across a wide range of platforms.
  294.  
  295. @dl_library_path should also be initialised with any other directories
  296. that can be determined from the environment at runtime (such as
  297. LD_LIBRARY_PATH for SunOS).
  298.  
  299. After initialisation @dl_library_path can be manipulated by an
  300. application using push and unshift before calling dl_findfile().
  301. Unshift can be used to add directories to the front of the search order
  302. either to save search time or to override libraries with the same name
  303. in the 'normal' directories.
  304.  
  305. The load function that dl_load_file() calls may require an absolute
  306. pathname.  The dl_findfile() function and @dl_library_path can be
  307. used to search for and return the absolute pathname for the
  308. library/object that you wish to load.
  309.  
  310. =item @dl_resolve_using
  311.  
  312. A list of additional libraries or other shared objects which can be
  313. used to resolve any undefined symbols that might be generated by a
  314. later call to load_file().
  315.  
  316. This is only required on some platforms which do not handle dependent
  317. libraries automatically.  For example the Socket Perl extension
  318. library (F<auto/Socket/Socket.so>) contains references to many socket
  319. functions which need to be resolved when it's loaded.  Most platforms
  320. will automatically know where to find the 'dependent' library (e.g.,
  321. F</usr/lib/libsocket.so>).  A few platforms need to be told the
  322. location of the dependent library explicitly.  Use @dl_resolve_using
  323. for this.
  324.  
  325. Example usage:
  326.  
  327.     @dl_resolve_using = dl_findfile('-lsocket');
  328.  
  329. =item @dl_require_symbols
  330.  
  331. A list of one or more symbol names that are in the library/object file
  332. to be dynamically loaded.  This is only required on some platforms.
  333.  
  334. =item @dl_librefs
  335.  
  336. An array of the handles returned by successful calls to dl_load_file(),
  337. made by bootstrap, in the order in which they were loaded.
  338. Can be used with dl_find_symbol() to look for a symbol in any of
  339. the loaded files.
  340.  
  341. =item @dl_modules
  342.  
  343. An array of module (package) names that have been bootstrap'ed.
  344.  
  345. =item dl_error()
  346.  
  347. Syntax:
  348.  
  349.     $message = dl_error();
  350.  
  351. Error message text from the last failed DynaLoader function.  Note
  352. that, similar to errno in unix, a successful function call does not
  353. reset this message.
  354.  
  355. Implementations should detect the error as soon as it occurs in any of
  356. the other functions and save the corresponding message for later
  357. retrieval.  This will avoid problems on some platforms (such as SunOS)
  358. where the error message is very temporary (e.g., dlerror()).
  359.  
  360. =item $dl_debug
  361.  
  362. Internal debugging messages are enabled when $dl_debug is set true.
  363. Currently setting $dl_debug only affects the Perl side of the
  364. DynaLoader.  These messages should help an application developer to
  365. resolve any DynaLoader usage problems.
  366.  
  367. $dl_debug is set to C<$ENV{'PERL_DL_DEBUG'}> if defined.
  368.  
  369. For the DynaLoader developer/porter there is a similar debugging
  370. variable added to the C code (see dlutils.c) and enabled if Perl was
  371. built with the B<-DDEBUGGING> flag.  This can also be set via the
  372. PERL_DL_DEBUG environment variable.  Set to 1 for minimal information or
  373. higher for more.
  374.  
  375. =item dl_findfile()
  376.  
  377. Syntax:
  378.  
  379.     @filepaths = dl_findfile(@names)
  380.  
  381. Determine the full paths (including file suffix) of one or more
  382. loadable files given their generic names and optionally one or more
  383. directories.  Searches directories in @dl_library_path by default and
  384. returns an empty list if no files were found.
  385.  
  386. Names can be specified in a variety of platform independent forms.  Any
  387. names in the form B<-lname> are converted into F<libname.*>, where F<.*> is
  388. an appropriate suffix for the platform.
  389.  
  390. If a name does not already have a suitable prefix and/or suffix then
  391. the corresponding file will be searched for by trying combinations of
  392. prefix and suffix appropriate to the platform: "$name.o", "lib$name.*"
  393. and "$name".
  394.  
  395. If any directories are included in @names they are searched before
  396. @dl_library_path.  Directories may be specified as B<-Ldir>.  Any other
  397. names are treated as filenames to be searched for.
  398.  
  399. Using arguments of the form C<-Ldir> and C<-lname> is recommended.
  400.  
  401. Example: 
  402.  
  403.     @dl_resolve_using = dl_findfile(qw(-L/usr/5lib -lposix));
  404.  
  405.  
  406. =item dl_expandspec()
  407.  
  408. Syntax:
  409.  
  410.     $filepath = dl_expandspec($spec)
  411.  
  412. Some unusual systems, such as VMS, require special filename handling in
  413. order to deal with symbolic names for files (i.e., VMS's Logical Names).
  414.  
  415. To support these systems a dl_expandspec() function can be implemented
  416. either in the F<dl_*.xs> file or code can be added to the autoloadable
  417. dl_expandspec() function in F<DynaLoader.pm>.  See F<DynaLoader.pm> for
  418. more information.
  419.  
  420. =item dl_load_file()
  421.  
  422. Syntax:
  423.  
  424.     $libref = dl_load_file($filename, $flags)
  425.  
  426. Dynamically load $filename, which must be the path to a shared object
  427. or library.  An opaque 'library reference' is returned as a handle for
  428. the loaded object.  Returns undef on error.
  429.  
  430. The $flags argument to alters dl_load_file behaviour.  
  431. Assigned bits:
  432.  
  433.  0x01  make symbols available for linking later dl_load_file's.
  434.        (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL))
  435.        (ignored under VMS; this is a normal part of image linking)
  436.  
  437. (On systems that provide a handle for the loaded object such as SunOS
  438. and HPUX, $libref will be that handle.  On other systems $libref will
  439. typically be $filename or a pointer to a buffer containing $filename.
  440. The application should not examine or alter $libref in any way.)
  441.  
  442. This is the function that does the real work.  It should use the
  443. current values of @dl_require_symbols and @dl_resolve_using if required.
  444.  
  445.     SunOS: dlopen($filename)
  446.     HP-UX: shl_load($filename)
  447.     Linux: dld_create_reference(@dl_require_symbols); dld_link($filename)
  448.     NeXT:  rld_load($filename, @dl_resolve_using)
  449.     VMS:   lib$find_image_symbol($filename,$dl_require_symbols[0])
  450.  
  451. (The dlopen() function is also used by Solaris and some versions of
  452. Linux, and is a common choice when providing a "wrapper" on other
  453. mechanisms as is done in the OS/2 port.)
  454.  
  455. =item dl_loadflags()
  456.  
  457. Syntax:
  458.  
  459.     $flags = dl_loadflags $modulename;
  460.  
  461. Designed to be a method call, and to be overridden by a derived class
  462. (i.e. a class which has DynaLoader in its @ISA).  The definition in
  463. DynaLoader itself returns 0, which produces standard behavior from
  464. dl_load_file().
  465.  
  466. =item dl_find_symbol()
  467.  
  468. Syntax:
  469.  
  470.     $symref = dl_find_symbol($libref, $symbol)
  471.  
  472. Return the address of the symbol $symbol or C<undef> if not found.  If the
  473. target system has separate functions to search for symbols of different
  474. types then dl_find_symbol() should search for function symbols first and
  475. then other types.
  476.  
  477. The exact manner in which the address is returned in $symref is not
  478. currently defined.  The only initial requirement is that $symref can
  479. be passed to, and understood by, dl_install_xsub().
  480.  
  481.     SunOS: dlsym($libref, $symbol)
  482.     HP-UX: shl_findsym($libref, $symbol)
  483.     Linux: dld_get_func($symbol) and/or dld_get_symbol($symbol)
  484.     NeXT:  rld_lookup("_$symbol")
  485.     VMS:   lib$find_image_symbol($libref,$symbol)
  486.  
  487.  
  488. =item dl_find_symbol_anywhere()
  489.  
  490. Syntax:
  491.  
  492.     $symref = dl_find_symbol_anywhere($symbol)
  493.  
  494. Applies dl_find_symbol() to the members of @dl_librefs and returns
  495. the first match found.
  496.  
  497. =item dl_undef_symbols()
  498.  
  499. Example
  500.  
  501.     @symbols = dl_undef_symbols()
  502.  
  503. Return a list of symbol names which remain undefined after load_file().
  504. Returns C<()> if not known.  Don't worry if your platform does not provide
  505. a mechanism for this.  Most do not need it and hence do not provide it,
  506. they just return an empty list.
  507.  
  508.  
  509. =item dl_install_xsub()
  510.  
  511. Syntax:
  512.  
  513.     dl_install_xsub($perl_name, $symref [, $filename])
  514.  
  515. Create a new Perl external subroutine named $perl_name using $symref as
  516. a pointer to the function which implements the routine.  This is simply
  517. a direct call to newXSUB().  Returns a reference to the installed
  518. function.
  519.  
  520. The $filename parameter is used by Perl to identify the source file for
  521. the function if required by die(), caller() or the debugger.  If
  522. $filename is not defined then "DynaLoader" will be used.
  523.  
  524.  
  525. =item bootstrap()
  526.  
  527. Syntax:
  528.  
  529. bootstrap($module)
  530.  
  531. This is the normal entry point for automatic dynamic loading in Perl.
  532.  
  533. It performs the following actions:
  534.  
  535. =over 8
  536.  
  537. =item *
  538.  
  539. locates an auto/$module directory by searching @INC
  540.  
  541. =item *
  542.  
  543. uses dl_findfile() to determine the filename to load
  544.  
  545. =item *
  546.  
  547. sets @dl_require_symbols to C<("boot_$module")>
  548.  
  549. =item *
  550.  
  551. executes an F<auto/$module/$module.bs> file if it exists
  552. (typically used to add to @dl_resolve_using any files which
  553. are required to load the module on the current platform)
  554.  
  555. =item *
  556.  
  557. calls dl_load_flags() to determine how to load the file.
  558.  
  559. =item *
  560.  
  561. calls dl_load_file() to load the file
  562.  
  563. =item *
  564.  
  565. calls dl_undef_symbols() and warns if any symbols are undefined
  566.  
  567. =item *
  568.  
  569. calls dl_find_symbol() for "boot_$module"
  570.  
  571. =item *
  572.  
  573. calls dl_install_xsub() to install it as "${module}::bootstrap"
  574.  
  575. =item *
  576.  
  577. calls &{"${module}::bootstrap"} to bootstrap the module (actually
  578. it uses the function reference returned by dl_install_xsub for speed)
  579.  
  580. =back
  581.  
  582. =back
  583.  
  584.  
  585. =head1 AUTHOR
  586.  
  587. Tim Bunce, 11 August 1994.
  588.  
  589. This interface is based on the work and comments of (in no particular
  590. order): Larry Wall, Robert Sanders, Dean Roehrich, Jeff Okamoto, Anno
  591. Siegel, Thomas Neumann, Paul Marquess, Charles Bailey, myself and others.
  592.  
  593. Larry Wall designed the elegant inherited bootstrap mechanism and
  594. implemented the first Perl 5 dynamic loader using it.
  595.  
  596. Solaris global loading added by Nick Ing-Simmons with design/coding
  597. assistance from Tim Bunce, January 1996.
  598.  
  599. =cut
  600.